package com.geekhale.validate.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

@Slf4j
public class CheckByServiceValidator implements ConstraintValidator<ValidByService, Object> {
    private ApplicationContext applicationContext;


    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        log.info("application {}", this.applicationContext);
        if (value instanceof User){
            User user = (User) value;
            if (user.getName().equals(user.getPwd())) {
                context.buildConstraintViolationWithTemplate("姓名密码不能一致");
                return false;
            }
            return true;
        }
        return true;
    }

    @Autowired
    public void setApplicationContext(ApplicationContext applicationContext){
        this.applicationContext = applicationContext;
    }
}
